package Qsharecom;

use WWW::Mechanize;
use HTTP::Cookies;

$| = 1;

sub upload
{
    my $local_file = shift;
	my $hoster_config = shift;

    my $mech = WWW::Mechanize->new();

    my $cookie_jar = HTTP::Cookies->new(
            file => $::spreader_cookie_dir."qsharecom.dat",
            autosave => 1,
        );

    $mech->cookie_jar($cookie_jar);

    my $textbox_content =  "ftp://".$::spreader_ftp_user.":".$::spreader_ftp_pwd."\@".$::spreader_ftp_ip.$::spreader_ftp_path.$local_file."\n";

    $mech->get("http://s1.qshare.com/index.php?sysm=user_portal&sysf=login");

    if($mech->content =~ /get_qry/)
    {
        $mech->form_number(1);
        $mech->field("sysm" => $mech->value("sysm",1));
        $mech->field("sysf" => $mech->value("sysf",1));
        $mech->field("get_qry" => $mech->value("get_qry",1));
        $mech->field("post_qry" => $mech->value("post_qry",1));
        $mech->field("target" => $mech->value("target",1));
        $mech->field("username" => $hoster_config->{'username'});
        $mech->field("password" => $hoster_config->{'password'});
        $mech->field("cookie" => "1");
        $mech->submit();	
    }

    $mech->get("http://s1.qshare.com/index.php?sysm=file_transfer&sysf=remotes");

    $mech->form_number(1);
    $mech->field("sysm" => $mech->value("sysm",1));
    $mech->field("sysf" => $mech->value("sysf",1));
    $mech->field("cmd" => $mech->value("cmd",1));
    $mech->field("urls" => $textbox_content);
    $mech->submit();	

    $mech->get("http://s1.qshare.com/index.php?sysm=file_transfer&sysf=remotes");

    my $in_process = 1;

    while($in_process)
    {
        $mech->get("http://s1.qshare.com/index.php?sysm=user_adm&sysf=admin");

        if($mech->content =~ m!(http://.*?speedtest\.zip)!)
        {
            $in_process = 0;
            return $1;
        }
        else
        {
            print "Noch nicht abgeschlossen. Warte ".$hoster_config->{'sleep_linkfetch'}." Sekunden.\n";
            sleep($hoster_config->{'sleep_linkfetch'});
        }
    }
}
1;
